package com.quick.threadtest;

import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CyclicBarrier;

/**
 * @ClassName TTTTTT
 * @Description TODO
 * @Author Wangxin
 * @Date 2019-02-21 22:02
 * @Version 1.0
 **/
public class BarrierActionTest {
    public static volatile CopyOnWriteArrayList<String> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(6,()->{
            System.out.println(copyOnWriteArrayList.toString());
        });
        for (int i = 0; i < 3; i++) {
            Thread thread1 = new Thread(new Tth1(cyclicBarrier));
            Thread thread2 = new Thread(new Tth2(cyclicBarrier));
            thread1.start();
            thread2.start();
        }

    }
}

class Tth1 implements Runnable{
    private CyclicBarrier cyclicBarrier;
    public Tth1(CyclicBarrier cyclicBarrier){
        this.cyclicBarrier = cyclicBarrier;
    }

    @Override
    public void run() {
        try {
            BarrierActionTest.copyOnWriteArrayList.add("aaa");
            cyclicBarrier.await();
        }catch (Exception e){

        }
    }
}

class Tth2 implements Runnable{
    private CyclicBarrier cyclicBarrier;
    public Tth2(CyclicBarrier cyclicBarrier){
        this.cyclicBarrier = cyclicBarrier;
    }
    @Override
    public void run() {
        try {
            BarrierActionTest.copyOnWriteArrayList.add("bb");
            cyclicBarrier.await();
        }catch (Exception e){

        }

    }
}
